//
//  GLBadgeViewController.m
//  KTools
//
//  Created by zhiyong.kuang on 2017/9/22.
//  Copyright © 2017年 zhiyong.kuang. All rights reserved.
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//                  佛祖镇楼                  BUG辟易

#import "GLBadgeViewController.h"
#import "GLBadgeLabel.h"
#import <Masonry/Masonry.h>
#import "UIViewController+GLForm.h"

@interface GLBadgeViewController ()
@property(nonatomic,strong)NSArray* datas;
@end

@implementation GLBadgeViewController

-(void)viewDidLoad{
    
    [super viewDidLoad];
    self.datas = @[@{@"keyPath":@"com.app.news"},
                             @{@"keyPath":@"com.app.mall"},
                             @{@"keyPath":@"com.app"},
                             @{@"keyPath":@"com.car.conn"},
                             @{@"keyPath":@"com"},
                             @{@"keyPath":@"com.app.news.tiyu",@"value":@88},
                             @{@"keyPath":@"com.app.news.keji",@"value":@1},
                             @{@"keyPath":@"com.app.news.yule",@"value":@2},
                             @{@"keyPath":@"com.app.mall.number",@"value":@3},
                             @{@"keyPath":@"com.app.mall.update",@"value":@79},
                             @{@"keyPath":@"com.car.conn.number",@"value":@3},
                             @{@"keyPath":@"com.car.conn.test",@"value":@3},
                             @{@"keyPath":@"com.car.conn.test2",@"value":@3},
                             @{@"keyPath":@"com.car.conn.number",@"value":@3},
                             ];
    
    [self gl_registerForm:[self form0]];
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"保存" forState:UIControlStateNormal];
//btn.badgeLabel.style = GLBadgeLabelStyleDot;
//    [btn showBadgeBindKeyPath:@"com.app" center:CGPointZero badgeColor:[UIColor redColor]];
    [btn showBadgeStyle:GLBadgeStyleDot bindKeyPath:@"com.app" centerOffset:CGPointZero badgeColor:[UIColor redColor]];
//    [btn showBadgeStyle:GLBadgeStyleDot bindKeyPath:@"com.app" center:CGPointZero badgeColor:[UIColor redColor]];
    
    UIBarButtonItem* item = [[UIBarButtonItem alloc]initWithCustomView:btn];
    //UIBarButtonItem* item = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];

    
    self.navigationItem.rightBarButtonItem = item;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        for (NSDictionary* dict in self.datas) {
            NSString* title = dict[@"keyPath"];
            NSNumber* value = dict[@"value"];
            if (value) {
                [[GLBadgeCenter defaultCenter]setBadgeValue:[value integerValue] forKeyPath:title];
            }
        }
    });
    
    
}
-(void)dealloc{
    NSLog(@"");
}



-(GLForm*)form0{
    GLForm* form = [GLForm formWithTag:@"form"];
    
    GLFormSection* section = [GLFormSection formSectionWithTag:@""];
    
    
    for (NSDictionary* dict in self.datas) {
        NSString* title = dict[@"keyPath"];
        
        NSRegularExpression* ex = [NSRegularExpression regularExpressionWithPattern:@"\\." options:NSRegularExpressionCaseInsensitive error:nil];
        NSArray* result = [ex matchesInString:title options:NSMatchingReportCompletion range:NSMakeRange(0, title.length)];
        if (result.count >= 3) {
            GLFormRow* row1 = [GLFormRow formRowByComponentKeys:@[GLCustomCellText,GLCustomCellButton,GLCustomCellButton2,GLCustomCellCatpionText,GLCustomCellBadge] withTag:@"cell"];
            [row1 addConfigValue:title componentKey:GLCustomCellText forKeyPath:nil];
            [row1 addConfigValue:title componentKey:GLCustomCellBadge forKeyPath:@"bindKeyPath"];
            [row1 addConfigValue:@"加" forKeyPath:GLCustomCellButton];
            [row1 addConfigValue:@"减" forKeyPath:GLCustomCellButton2];
            [section addFormRow:row1];
        }else{
            GLFormRow* row1 = [GLFormRow formRowByComponentKeys:@[GLCustomCellText,GLCustomCellBadge] withTag:@"cell"];
            [row1 addConfigValue:title componentKey:GLCustomCellText forKeyPath:nil];
            [row1 addConfigValue:title componentKey:GLCustomCellBadge forKeyPath:@"bindKeyPath"];
            
            NSInteger index = [self.datas indexOfObject:dict];
            NSInteger i = index%(GLBadgeStyleCustom);
            [row1 addConfigValue:@(i) componentKey:GLCustomCellBadge forKeyPath:@"style"];

            
            [section addFormRow:row1];
        }
        
        
    }
    
    [form addFormSection:section];
    
    __weak typeof(self)weakSelf = self;
    form.formClicked = ^(GLFormRow *row, id sender, BOOL isSub, NSString *subTag) {
        if (isSub) {
            NSLog(@"eventClicked--->%@---%@",sender,subTag);
            
            NSIndexPath* indexPath = [weakSelf.gl_form indexPathForFormRow:row];
            NSDictionary* dict = [weakSelf.datas objectAtIndex:indexPath.row];
            NSString* keyPath = dict[@"keyPath"];
            NSInteger value = [[GLBadgeCenter defaultCenter]badgeValueForKeyPath:keyPath];
            
            if ([subTag isEqualToString:GLCustomCellButton]) {
                value++;
            }
            else{
                value--;
            }
            if (value < 0) {
                return;
            }
            [[GLBadgeCenter defaultCenter]setBadgeValue:value forKeyPath:keyPath];
        }
        
    };
    return form;
    
}




@end
